package com.tangdi.production.mpaychl.service.impl.bncp;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.tangdi.production.mpaychl.base.service.BaseTranService;
import com.tangdi.production.mpaychl.service.impl.bncp.util.MD5Util;
import com.tangdi.production.mpbase.constants.MsgCT;
import com.tangdi.production.mpbase.exception.TranException;
import com.tangdi.production.mpbase.util.MoneyUtils;
import com.tangdi.production.mpbase.util.ObjectUtil;

@Service("BNCP_service2")
public class BncpServiceImpl2 implements BaseTranService {
	private static final Logger logger = LoggerFactory.getLogger(BncpServiceImpl2.class);
	
//	private static final String URL = "http://www.bncp.xyz/apisubmit";
	private static final String URL = "http://www.bncp.in/apisubmit";
	
//	private static final String CUSTOMER_USER_NAME = "BNCP1188022808";
//	private static final String CUSTOMER_ID = "10984";
//	public static final String API_KEY = "e2ede07494668d237d6a9c7f89c241a4cf9a2579";
	private static final String CUSTOMER_ID = "11002";
	public static final String API_KEY = "0585d008825764fbd9fdad646d89cfb3e1adefcb";
	
	private static final String NOTIFY_URL = "http://ce.imbitpay.com:8083/mpcctp/bncp/notify-url.json";
//	private static final String RETURN_URL = "";
	private static final String CURRENCY = "CNY";
	private static final String PAY_TYPE = "bank";
	private static final String BANK_CODE = "ICBC";
	
	@Override
	public Map<String, Object> execute(Map<String, Object> param) throws TranException {
		logger.info("进入BNCP_service2处理接口,参数:[{}]", param);
		try {
			String version = "1.0";
			String customerid = CUSTOMER_ID;
			String sdorderno = "" + param.get("prdordno");
			String payAmt = ObjectUtil.getString(param.get("payAmt"));
			logger.info("支付金额[{}分]", payAmt);
			payAmt = MoneyUtils.toStrYuan(payAmt);
			logger.info("支付金额转换为元[{}]", payAmt);
			String number = payAmt;
			String currency = CURRENCY;
			String paytype = PAY_TYPE;
			String bankcode = BANK_CODE;
			String notifyurl = NOTIFY_URL;
			String returnurl = "" + param.get("frontUrl");
			String remark = "" + param.get("orderDesc");
			
			String encodeStr = "version=".concat(version)
					.concat("&").concat("currency=").concat(currency)
					.concat("&").concat("customerid=").concat(customerid)
					.concat("&").concat("sdorderno=").concat(sdorderno)
					.concat("&").concat("notifyurl=").concat(notifyurl)
					.concat("&").concat("returnurl=").concat(returnurl)
					.concat("&").concat(API_KEY);
			
			String sign = MD5Util.encode(encodeStr.getBytes());
			
			StringBuilder returtnHtml = new StringBuilder();
			returtnHtml.append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\" /></head><body><script language=\"javascript\">window.onload=function(){document.payfrm.submit();}</script>");
			returtnHtml.append("<form id=\"payfrm\" name=\"payfrm\" action=\"").append(URL).append("\" method=\"post\" >");
			returtnHtml.append("<input type=\"hidden\" name=\"version\" value=\"").append(version).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"customerid\" value=\"").append(customerid).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"sdorderno\" value=\"").append(sdorderno).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"number\" value=\"").append(number).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"currency\" value=\"").append(currency).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"paytype\" value=\"").append(paytype).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"bankcode\" value=\"").append(bankcode).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"notifyurl\" value=\"").append(notifyurl).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"returnurl\" value=\"").append(returnurl).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"remark\" value=\"").append(remark).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"sign\" value=\"").append(sign).append("\" />");
			returtnHtml.append("</form></body></html>");
			
			Map<String,Object> backMap = new HashMap<>();
			backMap.put("ordStatus", MsgCT.ORDSTATUS_PROCESSING);
			backMap.put("contentType", MsgCT.DATA_TYPE_HTML);
			backMap.put("busContent", returtnHtml.toString());
			return backMap;
		} catch (Exception e) {
			logger.error("渠道BNCP2调用异常:{}", e.getMessage(), e);
			Map<String,Object> backMap = new HashMap<>();
			backMap.put("ordStatus", MsgCT.ORDSTATUS_FAIL);
			backMap.put("msg", "渠道BNCP2调用异常:" + e.getMessage());
			return backMap;
		}
	}

}
